package com.skydoves.preferenceroomdemo;

import com.skydoves.preferenceroomdemo.components.PreferenceComponent_AppComponent;
import com.skydoves.preferenceroomdemo.models.ItemProfile;
import com.skydoves.preferenceroom.InjectPreference;
import com.skydoves.preferenceroomdemo.utils.ListViewProvider;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

public class MainAbility extends Ability {
    @InjectPreference
    public PreferenceComponent_AppComponent mComponent;

    @Override
    public void onStart(final Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        PreferenceComponent_AppComponent.getInstance().inject(this);

        initializeUI();
        setProfileButton();
    }

    private void initializeUI() {
        ListViewProvider provider = new ListViewProvider(this);
        ListContainer listContainer = null;
        Component cpt = findComponentById(ResourceTable.Id_content_listView);
        if (cpt instanceof ListContainer) {
            listContainer = (ListContainer) cpt;
            if (mComponent.UserProfile().getLogin()) {
                provider.addItem(new ItemProfile(
                        "message",
                        mComponent.UserProfile().getNickname()));
                provider.addItem(new ItemProfile(
                        "nick value",
                        mComponent.UserProfile().getUserinfo().getName()));
                provider.addItem(new ItemProfile(
                        "age",
                        mComponent.UserProfile().getUserinfo().getAge() + ""));
                provider.addItem(new ItemProfile(
                        "visits",
                        mComponent.UserProfile().getVisits() + ""));
                mComponent.UserProfile().putVisits(
                        mComponent.UserProfile().getVisits());
            }
            if (mComponent.UserDevice().getUuid() == null) {
                putDumpDeviceInfo();
            } else {
                provider.addItem(new ItemProfile(
                        "version", mComponent.UserDevice().getVersion()));
                provider.addItem(new ItemProfile(
                        "uuid", mComponent.UserDevice().getUuid()));
            }
            listContainer.setItemProvider(provider);
        }
    }

    private void setProfileButton() {
        Button needLoginView = null;
        Component cpt = findComponentById(ResourceTable.Id_content_button);
        if (cpt instanceof Button) {
            needLoginView = (Button) cpt;
            needLoginView.setClickedListener(component -> {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.skydoves.preferenceroomdemo")
                        .withAbilityName(LoginAbility.class.getName())
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
            });
        }
    }

    private void putDumpDeviceInfo() {
        mComponent.UserDevice().putVersion("1.0.0.0");
        mComponent.UserDevice().putUuid("00001234-0000-0000-0000-000123456789");
    }

}